package com.huike.pmps.controller;


import com.huike.pmps.model.entity.Member;
import com.huike.pmps.model.vo.Result;
import com.huike.pmps.service.PermissionService;
import com.huike.pmps.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 作者：谌贵斌(黑桃K)
 * 日期: 2888-08-08
 * 老师QQ: 272488352
 * 官方QQ交流群: 377748272
 * 刀客程序员官网：http://www.daoke360.com
 * 刀客程序员淘宝旗舰店：https://daoke360.taobao.com/
 */
@Api(tags = "安全管理")
@RestController
@RequestMapping("/api/security")
public class SecurityController {

    @Autowired
    private ConsumerTokenServices consumerTokenServices;

    @ApiOperation(value = "获取用户所有详细信息", httpMethod = "GET")
    @GetMapping
    public Result getMember(HttpServletRequest request) {
        //从请求头中获取token
        Member member = (Member) request.getSession().getAttribute(TokenUtil.getToken(request));
        return Result.success(member);
    }

    @ApiOperation(value = "注销登录", httpMethod = "DELETE")
    @DeleteMapping
    public Result revokeToken(HttpServletRequest request) {
        String token=TokenUtil.getToken(request);
        request.getSession().removeAttribute(token);
        if (consumerTokenServices.revokeToken(token)) {
            return Result.success("注销成功");
        } else {
            return Result.success("注销失败");
        }
    }
}
